﻿#/bin/bash
#新建虚拟机使用iso镜像作为yum源

#准备提示
echo "运行本脚本前，请确保/dev/cdrom已经挂载！"

#主循环开始
while :
do
read -p '是否继续运行此脚本y/n' p
if [ $p == y ] ; then 

#挂载cdrom
[ -z /dev/cdrom ] &> /dev/null || echo "cdrom no exist! please check" && exit
mkdir /dvd
echo "/dev/cdrom /dvd iso9660 defaults 0 0 " >> /etc/fstab
echo "开机自动挂载---------ok！"
sleep 0.5

#将默认repo文件备份
mkdir /etc/yum.repos.d/repos
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo

#书写新的repo文件
echo "[Centos7]
name=Centos7--yum
baseurl=file:///dvd
enabled=1
gpgcheck=0" > /etc/yum.repos.d/Centos7.repo

#刷新配置
mount -a 
yum clean all
yum makecache
yum repolist

#检查yum源是否安装
i=`yum repolist | sed -n '$p' | sed 's/,//g' | awk '{print $2}'`
[ $i -le 9000 ] &> /dev/null && echo "请检查yum源是否正常" && exit
echo "检查yum源正常!"
echo "Finish!"
exit

#主循环判断末尾
elif [ $p == n ] ; then
	exit
else
	echo "请输入y/n!"
fi
done